package com.bite.springtrans.demos.service;

import com.bite.springtrans.demos.mapper.LogInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class LogService {
    @Autowired
    private LogInfoMapper logInfoMapper;


    @Transactional(propagation = Propagation.REQUIRED)
    public void insertLog(String userName,String op) {
        try {
            int a = 10/0;
        } catch (Exception e) {
            //分别使用 NESTED 和 REQUIRED 进行回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        //记录用户操作
        logInfoMapper.insert(userName,"用户操作");
    }


    @Transactional(propagation = Propagation.NESTED)
    public void insertLog1(String userName,String op) {
        try {
            int a = 10/0;
        } catch (Exception e) {
            //分别使用 NESTED 和 REQUIRED 进行回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        //记录用户操作
        logInfoMapper.insert(userName,"用户操作");
    }
}
